<template>
	<view v-if="!loading" class="body">
		<view class="head-box">
			<image class="avatar-img" :src="staticBaseUrl + '/mintai-icon.png'">
			</image>
		</view>
		<view class="auth-phone-box">
			<view class="agreement">
				<radio @tap="checkboxChange" :checked="checkedFlag" value="1" color="#FF4A4A"></radio>
				<!-- <checkbox value="1" :checked="checkedFlag" color="#FF4A4A" @click="checkboxChange" /> -->
				<view class="agreement-right">我已阅读<text class="agreement-link" @click="linkTo(1)">《民泰惠生活用户隐私政策》</text>、<text class="agreement-link" @click="linkTo(2)">《民泰惠生活平台服务协议》</text>并同意该手机号注册成为民泰惠生活会员</view>
			</view>
			<button class="active-btn" :disabled="isDisabled" v-if="phone" @tap="phoneLogin">进入民泰惠生活</button>
			<button class="active-btn" :disabled="isDisabled" v-else-if="!checkedFlag" @tap="noChecked">进入民泰惠生活</button>
			<button class="active-btn" :disabled="isDisabled" open-type="getPhoneNumber" v-else
			 @getphonenumber="getPhoneNumber">进入民泰惠生活</button>
			<!-- <button class="active-default" @tap.stop.prevent="goBack">暂不登录</button> -->
		</view>
		<!-- <view class="tips-area">
			<text class="tips-icon">*</text>
			<text class="tips-content">如您不是本系统的注册用户，点击“微信登录”即默认注册</text>
		</view> -->
		
	</view>
</template>

<script>
	const app = getApp();
	const utils = require('../../utils/util');
	import {
		staticBaseUrl
	} from '@/utils/constant.js'
	import { http } from '@/utils/commonRequest.js'

	export default {
		data() {
			return {
				staticBaseUrl,
				userInfo: null,
				inviterId: null,
				inviteActId: null,

				// 授权手机号按钮是否可点击
				isDisabled: false,

				qmhOpenIdData: '',
				loading: '',
				checkedFlag: false,
				phone: null,
				authCode: null
			};
		}
		/**
		 * 生命周期函数--监听页面加载
		 */
		,
		onLoad: function(options) {
			let that = this;

			if (options.inviterId && options.inviteActId) {
					that.inviterId = options.inviterId
					that.inviteActId = options.inviteActId
			}
			
			if(options.phone){
				that.phone = options.phone;
			}
			if(options.code){
				that.authCode = options.code;
			}

			that.userInfo = app.globalData.userInfo
		},
		onShow: function() {
		},
		methods: {
			/**
			 * 注册
			 */
			toRegist: function(phone, code) {
				var that = this;
				var params = {
					avatarUrl: that.userInfo && that.userInfo.avatarUrl,
					nickName: (that.userInfo && that.userInfo.nickName) || '微信用户',
					gender: (that.userInfo && that.userInfo.gender) || '1',
					phone: phone,
					code: code,
					inviterId: that.inviterId,
					inviteActId: that.inviteActId
				}; // 注册的时候判断平台是否是总分类型平台，请求不同注册接口

				let platType = app.globalData.platType;
				let url = '';
				params.miniOpenId = '';
				if (!utils.isEmpty(that.openIdData)) {
					params.openIdData = that.openIdData;
				}
				if (!utils.isEmpty(that.qmhOpenIdData)) {
					params.qmhOpenIdData = that.qmhOpenIdData;
				}
				if (platType == '3') {
					url = 'miniapp/regUserNew';
				} else {
					url = 'miniapp/regUser';
				}
				console.log("regist params:", params)
				http
					.post(url, params, {
						header: {
							"content-type": "application/x-www-form-urlencoded"
						}
					})
					.then((res) => {
						res = res.data
						uni.hideLoading();
						console.log(res);

						if ('200' == res.code) {
							if ('SUCCESS' == res.msg) {
								that.regSuccess(res);
							} else if ('SAME_PHONE' == res.msg) {
								uni.showModal({
									title: '温馨提示',
									content: '该手机号已注册，若您已注册请重新登录小程序',
									confirmColor: '#59A5F0',

									success(res) {
										if (res.confirm) {
											uni.setStorageSync('LOGIN_LOGIN', 'expire');
											uni.removeStorageSync('LOGIN_LASTTIME');
											uni.reLaunch({
												url: '/pages/index/index'
											});
										} else if (res.cancel) {
											this.$checkLogin().then((res) => {
												uni.setStorageSync('LOGIN_LOGIN', 'expire');
												uni.removeStorageSync('LOGIN_LASTTIME');
												uni.switchTab({
													url: '/pages/my/my'
												});
											});
										}
									}
								});
							} else if ('WRONG' == res.msg) {
								this.isDisabled = false
								utils.message('手机号不正确！');
							} else if ('ALREADY_EXISTS' == res.msg) {
								utils.message('请勿重复注册！');
								that.back();
							}
						} else {
							utils.message(res.msg);
							this.isDisabled = false
						}
					})
					.catch((err) => {
						uni.hideLoading();
						utils.message('登录失败，请稍后再试！');
						this.isDisabled = false
						console.log(err);
					});
			},

			regSuccess: function(res) {
				app.globalData.memberInfo = res.memberInfo;
				uni.showToast({
					title: '登录成功！'
				});
				uni.setStorageSync('LOGIN_LOGIN', 'authed');
				uni.removeStorageSync('QYPT_QMH_OPENID_DATA');
				this.back();
			},

			back: function() {
				let that = this
				setTimeout(function() {
					var navTypeAfterRegist = uni.getStorageSync('NAV_TYPE_AFTER_REGIST');
					var navUrlAfterRegist = uni.getStorageSync('NAV_URL_AFTER_REGIST');
console.log('???????==navTypeAfterRegist',navTypeAfterRegist)
					if ('redirect' == navTypeAfterRegist || 'navigateTo' == navTypeAfterRegist) {
						console.log('?????navUrlAfterRegist==',navUrlAfterRegist)
						uni.redirectTo({
							url: navUrlAfterRegist
						});
					} else if ('switchTab' == navTypeAfterRegist) {
						uni.switchTab({
							url: navUrlAfterRegist
						});
					} else {
						console.log('zheli zheli 这里')
						that.getOpenId()
					}
				}, 2000);
			},

			/**
			 * 获取手机号码
			 */
			getPhoneNumber: function(e) {
				console.log('获取手机号码==',e);
				if(!this.checkedFlag){
					utils.tips("请先阅读并勾选用户隐私政策和平台服务协议！")
					return
				}
				this.isDisabled = true
				uni.showLoading({
					title: '登录中,请稍后',
					mask: true
				});
				this.$checkLogin().then((res) => {
					http
						.get('/miniapp/getPhone', {
							params: {
								appid: uni.getAccountInfoSync().miniProgram.appId,
								code: e.detail.code
							}
						})
						.then((res) => {
							res = res.data
							if ('200' == res.code) {
								var phoneNumber = res.phoneNumber;
								this.toRegist(phoneNumber, 'WECHAT_PHONENUMBER');
							} else {
								uni.hideLoading();
								utils.message('登录失败，请稍后再试！');
								this.isDisabled = false
							}
						})
						.catch((err) => {
							uni.hideLoading();
							utils.message('登录失败，请稍后再试！');
							this.isDisabled = false
							console.log(err);
						});
				});
			},
			
			phoneLogin: function(){
				let that = this;
				if(!this.checkedFlag){
					utils.tips("请先阅读并勾选用户隐私政策和平台服务协议！")
					return
				}
				this.isDisabled = true
				uni.showLoading({
					title: '登录中,请稍后',
					mask: true
				});
				this.$checkLogin().then((res) => {
					uni.hideLoading();
					that.toRegist(that.phone, that.authCode);
					this.isDisabled = false
				}).catch((err) => {
						uni.hideLoading();
						utils.message('登录失败，请稍后再试！');
						this.isDisabled = false
						console.log(err);
					});
			},

			noChecked: function(){
				utils.tips("请先阅读并勾选用户隐私政策和平台服务协议！")
				return
			},

			goBack: function(e) {
				uni.navigateBack({
					delta: 1,
					fail: (err) => {
						uni.reLaunch({
							url: '/pages/index/index'
						});
					}
				});
			},
			
			checkboxChange: function() {
				this.checkedFlag = !this.checkedFlag;
			},
			
			linkTo: function(type){
				console.log('=====',type)
				uni.navigateTo({
					url: '/pages/agreement/agreement?type=' + type
				});
			},
			
			//登录完根据是否是分享进来的判断,跳转到哪个页面
			getOpenId: function() {
				return new Promise((resolve, reject) => {
					let outlink = uni.getStorageSync('outlink')
					let navUrl = encodeURIComponent(outlink);
					http.get('/taiHuiWan/getMemberUserMiniOpenId', {}).then(res => {
						uni.setStorageSync('MiniOpenId', res.data.MiniOpenId)
						if(navUrl) {
							uni.navigateTo({
								url: '/pages/outlink/outlink?outlink=' + navUrl
							})
							uni.removeStorageSync('outlink')
						} else {
							uni.navigateBack({
								delta: 1,
								fail: (err) => {
									uni.reLaunch({
										url: '/pages/index/index'
									});
								}
							});
						}
					})
					resolve()
				})
			}
		}
	};
</script>
<style>
	.agreement-right{
		margin-left: 10rpx;
	}
	.agreement{
		display: flex;
		margin-bottom: 80rpx;
		padding-left: 20rpx;
		color: #666;
	}
	
	.agreement-link{
		color: #0096FF;
		font-size: 26rpx;
		text-decoration: underline;
	}
	.body {
		height: 100vh;
		/* min-height: 100%; */
		background-color: white;
	}

	.head-box {
		/* padding-top: 130rpx; */
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	
	.avatar-img {
		/* width: 648rpx; */
		height: 100vh;
	}


	.active-btn {
		height: 90rpx;
		border-radius: 45rpx;
		font-size: 36rpx;
		background-color: #FF4A4A;
		color: #fff;
		/* margin: 0 60rpx; */
	}

	.active-default {
		height: 90rpx;
		border-radius: 45rpx;
		font-size: 36rpx;
		border: 1px solid #0096ff;
		background-color: #fefefe;
		color: #0096ff;
		margin: 0 60rpx;
		margin-top: 50rpx;
	}

	.auth-phone-box {
		/* margin-top: 390rpx; */
		position: absolute;
		bottom: 58rpx;
		left: 20rpx;
		right: 20rpx;
	}

	.tips-area {
		font-size: 26rpx;
		padding: 40rpx 20rpx;
		display: flex;
		justify-content: center;
	}

	.tips-icon {
		color: #ff5151;
	}

	.tips-content {
		margin-left: 10rpx;
		color: #9a9a9a;
	}
</style>
